<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./a.js"></script>
</head>
<body>
    <!-- <form action="" enctype="application/x-www-form-urlencoded"></form> -->
    <h1>我是主页</h1>
    <button class="btn1">点击我发送get请求</button>
    <button class="btn2">点击我发送post参数</button>

    <script>
        // ajax : async javascript and xml
        // json xml 主流的数据传输格式
        document.querySelector(".btn1").onclick = function(){
            let xhr = new XMLHttpRequest();
            xhr.open("get","/getdata?name=hello");
            xhr.onload = function(){
                console.log(xhr.responseText)
            }
            xhr.send();
        }

        document.querySelector(".btn2").onclick = function(){
            let xhr = new XMLHttpRequest();
            xhr.open("post","/postdata");
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            xhr.onload = function(){
                console.log(xhr.responseText)
            }
            let str = "name=zhangsan&age=20"
            xhr.send(str);
        }



    </script>
</body>
</html>